package cn.itcast.service;

import cn.itcast.dao.RouteDao;
import cn.itcast.domain.Route;
import cn.itcast.utils.SqlSessionOperateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RouteServiceImpl implements RouteService {

    @Override
    public PageInfo findPage(int currentPageNum, int pageSize) {
        SqlSession sqlSession = null;

        try {
            sqlSession = SqlSessionOperateUtil.getSqlSession();
            RouteDao routeDao = sqlSession.getMapper(RouteDao.class);

            //在findAll之前调用相应方法
            PageHelper.startPage(currentPageNum, pageSize);

            List<Route> routes = routeDao.findAll();
            SqlSessionOperateUtil.commit(sqlSession);

            return new PageInfo<>(routes);


        } catch (Exception e) {
            SqlSessionOperateUtil.rollback(sqlSession);
            e.printStackTrace();

        } finally {
            SqlSessionOperateUtil.close(sqlSession);

        }

        return null;

    }

    //查询
    public List<Route> findAll() {

        SqlSession sqlSession = null;

        try {
            sqlSession = SqlSessionOperateUtil.getSqlSession();
            RouteDao routeDao = sqlSession.getMapper(RouteDao.class);

            List<Route> routes = routeDao.findAll();
            SqlSessionOperateUtil.commit(sqlSession);
            return routes;

        } catch (Exception e) {
            SqlSessionOperateUtil.rollback(sqlSession);
            e.printStackTrace();

        } finally {
            SqlSessionOperateUtil.close(sqlSession);

        }

        return null;
    }

    // 根据id获取旅游线路
    public Route updateFindById(String rid) {
        SqlSession sqlSession = null;

        try {
            sqlSession = SqlSessionOperateUtil.getSqlSession();
            RouteDao routeDao = sqlSession.getMapper(RouteDao.class);

            Route route = routeDao.updateFindById(rid);

            //成功时提交
            SqlSessionOperateUtil.commit(sqlSession);

            return route;

        } catch (Exception e) {
            //失败时回滚
            SqlSessionOperateUtil.rollback(sqlSession);
            e.printStackTrace();

        } finally {
            //关闭资源
            SqlSessionOperateUtil.close(sqlSession);

        }

        return null;
    }

    //删除
    public void del(String rid) {
        SqlSession sqlSession = null;

        try {
            //获取Dao的代理对象
            sqlSession = SqlSessionOperateUtil.getSqlSession();
            RouteDao routeDao = sqlSession.getMapper(RouteDao.class);

            //具体操作
            routeDao.del(rid);

            //成功时提交
            SqlSessionOperateUtil.commit(sqlSession);


        } catch (Exception e) {
            //失败时回滚
            SqlSessionOperateUtil.rollback(sqlSession);
            e.printStackTrace();

        } finally {
            //关闭资源
            SqlSessionOperateUtil.close(sqlSession);

        }

    }

    //添加
    public void add(Route route) {
        SqlSession sqlSession = null;

        try {
            //获取Dao的代理对象
            sqlSession = SqlSessionOperateUtil.getSqlSession();
            RouteDao routeDao = sqlSession.getMapper(RouteDao.class);

            //具体操作
            routeDao.add(route);

            //成功时提交
            SqlSessionOperateUtil.commit(sqlSession);


        } catch (Exception e) {
            //失败时回滚
            SqlSessionOperateUtil.rollback(sqlSession);
            e.printStackTrace();

        } finally {
            //关闭资源
            SqlSessionOperateUtil.close(sqlSession);

        }

    }

    //修改
    public void update(Route route) {
        SqlSession sqlSession = null;

        try {
            //获取Dao的代理对象
            sqlSession = SqlSessionOperateUtil.getSqlSession();
            RouteDao routeDao = sqlSession.getMapper(RouteDao.class);

            //具体操作
            routeDao.update(route);

            //成功时提交
            SqlSessionOperateUtil.commit(sqlSession);


        } catch (Exception e) {
            //失败时回滚
            SqlSessionOperateUtil.rollback(sqlSession);
            e.printStackTrace();

        } finally {
            //关闭资源
            SqlSessionOperateUtil.close(sqlSession);

        }
    }
}
